package com.wlhse.controller;

import com.wlhse.cache.JedisClient;
import com.wlhse.dao.HSEDao;
import com.wlhse.dto.*;
import com.wlhse.dto.inDto.HseAuditGroupMembersDtos;
import com.wlhse.dto.inDto.QHSEECheckRecords;
import com.wlhse.dto.inDto.QHseeCheckRecordDto;
import com.wlhse.entity.HseAuditTaskPojo;
import com.wlhse.service.HSEService;
import com.wlhse.util.R;
import com.wlhse.util.state_code.NR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.List;
import java.util.stream.Collectors;

@Api(value ="/hse")
@RestController("HSEController")
@RequestMapping("/api/v1/hse")
public class HSEController {
    @Resource
    private HSEService hseService;
    @Autowired
    private HSEDao hseDao;

    @Resource
    private JedisClient jedisClient;

    @ApiOperation(value = "melon新建审核组")
    @RequestMapping(value = "/addHSEAuditGroup",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String addHSEAuditGroup(@RequestBody(required = false) HSEAuditGroupDto hseAuditGroupDto){
        return hseService.addHSEAuditGroup(hseAuditGroupDto);
    }


    //年份轮次查询审核组 现在的修改
    @RequestMapping(value = "/queryHSEAuditGroup",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryHSEAuditGroup(@ModelAttribute HSEAuditGroupDto hseAuditGroupDto){
        return hseService.queryHSEAuditGroup(hseAuditGroupDto);
    }


    @ApiOperation(value = "melon-删除审核组")
   @RequestMapping(value = "/deleteHSEAuditGroup",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
   public String deleteHSEAuditGroup(@RequestParam(value = "auditGroupID", required = false)Integer auditGroupID){
       return hseService.deleteHSEAuditGroup(auditGroupID);
   }
    @RequestMapping(value = "/deleteHSEAuditGroup/{auditGroupID}",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
    public String deleteHSEAuditGroups(@PathVariable Integer auditGroupID){
        return hseService.deleteHSEAuditGroup(auditGroupID);
    }
    //年份轮次查询审核组成员
    @RequestMapping(value = "/queryAllGroupMembersByYearRound",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryAllGroupMembersByYearRound(@ModelAttribute HSEAuditGroupDto hseAuditGroupDto, HttpServletRequest request){
        return hseService.queryAllGroupMembersByYearRound(hseAuditGroupDto,request);
    }

    //新建任务
    @RequestMapping(value = "/addHseAuditTask",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String addHseAuditTask(@RequestBody(required = false)HseAuditTaskPojo hseAuditTaskPojo, HttpServletRequest request){
        return hseService.addHseAuditTask(hseAuditTaskPojo,request);
    }

    //年份轮次查询任务
    @RequestMapping(value = "/queryHseAuditTask",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryHseAuditTask(@RequestParam(value = "taskDate",required = false) String taskDate,@RequestParam(value = "round",required = false) String round,
                                    @RequestParam(value = "num",required = false) Integer num,@RequestParam(value = "size",required = false) Integer size){
        return hseService.queryHseAuditTask(taskDate,round,num,size);
    }

    //完成任务
    @RequestMapping(value = "/finishHseAuditTask",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String finishHseAuditTask(@ModelAttribute HseAuditTaskPojo hseAuditTaskPojo){
        return hseService.finishHseAuditTask(hseAuditTaskPojo);
    }

    //修改任务
    @RequestMapping(value = "/updateHseAuditTask",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String updateHseAuditTask(@RequestBody(required = false)HseAuditTaskPojo hseAuditTaskPojo){
        return hseService.updateHseAuditTask(hseAuditTaskPojo);
    }

    //增加检查组员工
    @RequestMapping(value = "/addHseAuditGroupMember",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String addHseAuditGroupMember(@RequestBody HseAuditGroupMembersDto hseAuditGroupMembersDto){
        return hseService.addHseAuditGroupMembers(hseAuditGroupMembersDto);
    }

    //删除任务
    @RequestMapping(value = "/deleteHseAuditTask",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
    public String deleteHseAuditTask(@RequestParam(value = "auditTaskID",required = false) Integer auditTaskID){
        return hseService.deleteHseAuditTask(auditTaskID);
    }

    @ApiOperation(value = "修改检查组员工")
    @RequestMapping(value = "/updateHseAuditGroupMember",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateHseAuditGroupMember(@RequestBody HseAuditGroupMembersDtos hseAuditGroupMembersDto){
        return hseService.updateHseAuditGroupMembers(hseAuditGroupMembersDto);
    }

    //修改检查组员工的topic
    @RequestMapping(value = "/updateHseAuditGroupMemberTopic",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateHseAuditGroupMemberTopic(@RequestBody HseAuditGroupMembersDto hseAuditGroupMembersDto){
        return hseService.updateHseAuditGroupMembersTopics(hseAuditGroupMembersDto);
    }

    @ApiOperation("melon-查询检查组topic")
    @RequestMapping(value = "/queryHseAuditTopic",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public R queryHseAuditTopic(@ModelAttribute HseAuditSystem hseAuditSystem){
        return hseService.queryHseAuditTopic(hseAuditSystem);
    }

    @ApiOperation(value = "查询审核项")
    @RequestMapping(value = "/queryHseAuditSystemCheckItems/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryHseAuditSystemCheckItems(@PathVariable("code") String code){
        return hseService.queryHseAuditSystemCheckItems(code);
    }

    @ApiOperation("查询审核内容和评分项")
    @RequestMapping(value = "/queryHseAuditSystemByCode/{code}",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryHseAuditSystemByCode(@PathVariable("code") String code,@RequestParam String status){
        return hseService.queryHseAuditSystemByCode(code,status);
    }

    //查询任务和检查组
    @RequestMapping(value = "/queryTaskAndGroupByStaffID",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryTaskAndGroupByStaffID(HttpServletRequest request,@RequestParam(value = "staffID",required = false) String staffID,
                                             @RequestParam(value = "round",required = false) String round,
                                             @RequestParam(value = "taskDate",required = false) String taskDate){
        if(staffID==null || "".equals(staffID)){
            String token = request.getHeader("Authorization");
            staffID=jedisClient.get(token);
        }
        return hseService.queryTaskAndGroupByStaffID(staffID,round,taskDate);
    }
    @ApiOperation(value = "melon根据登录人和审核项查询记录")
    @RequestMapping(value = "/queryAuditSubjectRecord",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryAuditSubjectRecord(HttpServletRequest request,@ModelAttribute QHSEECheckRecord qhseeCheckRecord){
            String token = request.getHeader("Authorization");
            String id=jedisClient.get(token);
        qhseeCheckRecord.setCheckerID(id);
        return hseService.queryAuditSubjectRecord(qhseeCheckRecord);
    }
    @ApiOperation(value = "melon根据登录人和状态记录")
    @RequestMapping(value = "/queryAuditSubjectRecordByStatus",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public String queryAuditSubjectRecordByStatus(HttpServletRequest request,@ModelAttribute HseCheckRecordDto hseCheckRecordDto){
        String token = request.getHeader("Authorization");
        String id=jedisClient.get(token);
        hseCheckRecordDto.setUserID(Integer.parseInt(id));
        return hseService.queryAuditSubjectRecordByStatus(hseCheckRecordDto);
    }

    //上传检查记录
    @ApiOperation(value ="melon上传检查记录" )
    @RequestMapping(value = "/addCheckRecord",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String addCheckRecord(@RequestBody(required = false) QHSEECheckRecord qhseeCheckRecord){
        return hseService.addCheckRecord(qhseeCheckRecord);
    }
    @ApiOperation(value = "提交后检查记录状态改变为已提交")
    @RequestMapping(value = "/updateCheckRecordStatus",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateCheckRecordStatus(@RequestParam String ids){
        return hseService.updateCheckRecordStatus(ids);
    }
    @ApiOperation(value ="melon修改检查记录" )
    @RequestMapping(value = "/updateCheckRecord",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateCheckRecord(@RequestBody(required = false) QHSEECheckRecords qhseeCheckRecord){

        return hseService.updateCheckRecord(qhseeCheckRecord);
    }
    @ApiOperation(value ="melon删除记录" )
    @RequestMapping(value = "/deleteCheckRecord",method = RequestMethod.DELETE,produces = "application/json; charset=utf-8")
    public String deleteCheckRecord(@RequestBody(required = false) QHSEECheckRecord qhseeCheckRecord){
        hseDao.deleteCheckRecord (qhseeCheckRecord.getQhseeCheckRecordID());
        return NR.r("删除成功");
    }

    //查询检查记录
    @RequestMapping(value = "/queryQhseeCheckRecord",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public R queryQhseeCheckRecord(@ModelAttribute QHSEECheckRecords qhseeCheckRecord){

        return hseService.queryQhseeCheckRecord(qhseeCheckRecord);
    }

    //修改检查记录的状态
    @RequestMapping(value = "/updateQhseeCheckRecordStatus",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateQhseeCheckRecordStatus(@ModelAttribute QHSEECheckRecord qhseeCheckRecord){

        return hseService.updateQhseeCheckRecordStatus(qhseeCheckRecord);
    }

    //根据传入的QHSEECheckRecordID批量删除
    @RequestMapping(value = "/deleteQHSEECheckRecord",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public String deleteQHSEECheckRecord(@RequestBody List<String> QHSEECheckRecordList){
        return hseService.deleteQHSEECheckRecord(QHSEECheckRecordList);
    }

    @ApiOperation(value = "按检查单位、检查时间以及任务轮次求得个单位数量")
    @RequestMapping(value = "/queryQHSEECheckRecordNums",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public R queryQHSEECheckRecordNums(@ModelAttribute QHseeCheckRecordDto qhseeCheckRecord){
        return hseService.queryQHSEECheckRecordNums(qhseeCheckRecord);
    }
    @ApiOperation(value = "级联查询任务轮次以及审核组")
    @RequestMapping(value = "/cascadingQuery",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public R cascadingQuery(@RequestParam(required = false) String year,@RequestParam(required = false) String round,@RequestParam(required = false) String auditGroupName){
        return hseService.cascadingQuery(year,round,auditGroupName);
    }

}
